目前在做关于视频及流媒体播放项目时,有这样一个需求,应用启动时的 Splash 要求播放一段动画。其中视频播放的库使用是 Vitamio。
最开始要播放的文件比较大,有 18M+,在手机播放时画质非常好。但是一个 Splash 没有必要这么大,由于把视频压缩了下,变成了 1.5M+,这回文件大小是可以了,用电脑播放器播放时,画质和之前的大文件比没有太大的变化,但是在手机中播放时,却出现了很严重的失真现象。
这是怎么回事?最后经过调查,设置了一些参数,再次用手机播放时和电脑播放的效果一样了,没有出现失真的现象。现将完整解决方案与大家分享下:
说明:
代码中删除了和演示无关的业务代码。
测试用机: XiaoMi 2S Android 5.0.2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import android.app.Activity; import android.content.Intent; import android.media.AudioManager; import android.os.Bundle; import android.os.Handler;
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import io.vov.vitamio.MediaPlayer; import io.vov.vitamio.Vitamio; import io.vov.vitamio.widget.VideoView; public class SplashActivity extends Activity { private static final String TAG = SplashActivity.class.getSimpleName();
private static final int SPLASH_DISPLAY_LENGTH = 2500;
@Override public void onCreate(Bundle bundle) { super.onCreate(bundle); Vitamio.isInitialized(getApplicationContext()); InputStream ins = getResources().openRawResource(R.raw.splash_anim); File tmpFile = null; OutputStream output; try { tmpFile = File.createTempFile("splash", "mov"); output = new FileOutputStream(tmpFile); final byte[] buffer = new byte[102400]; int read; while ((read = ins.read(buffer)) != -1) { output.write(buffer, 0, read); } output.flush(); output.close(); ins.close(); } catch (IOException e) { e.printStackTrace(); } final io.vov.vitamio.widget.VideoView videoView = new VideoView(this); videoView.setHardwareDecoder(true); videoView.setVideoChroma(MediaPlayer.VIDEOCHROMA_RGB565);
videoView.setZOrderOnTop(true); videoView.setVideoPath(tmpFile.getPath()); videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) {
ALog.i(TAG, "onPreparedListener()"); videoView.setBackground(null); mediaPlayer.setAdaptiveStream(true); mediaPlayer.setPlaybackSpeed(1.0f); mediaPlayer.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH); setVolumeControlStream(AudioManager.STREAM_MUSIC); mediaPlayer.prepareAsync(); mediaPlayer.start(); } }); setContentView(videoView); new Handler().postDelayed(new Runnable() { @Override public void run() { ALog.d(TAG, "Ready to start initialization activity"); Intent intent = new Intent(SplashActivity.this, Mainctivity.class); startActivity(intent); finish(); } }, SPLASH_DISPLAY_LENGTH); } }
|
备注
Vitamio官网地址:https://www.vitamio.org/